  <title>Non Worker</title>
  <article>
    <p>Canvas is running whilst an prime number finder runs - this will cause your browser to hang</p>
    <p>Prime found: <span id="status">0</span></p>
    <div><input type="button" value="start worker" id="toggleWorker" /></div>
  </article>
<script>

buildSpinner({ x : 50, y : 50, size : 20, degrees : 30 });

var running = false;
var ctr = 0;

addEvent(document.getElementById('toggleWorker'), 'click', function (event) {
  // doesn't matter what the message is, just toggle the worker
  if (running == false) {
    running = true;
    run();
  } else {
    running = false;
  }
});

function run() {
  var n = 1;
  search: while (running) {
    n += 1;
    for (var i = 2; i <= Math.sqrt(n); i += 1)
      if (n % i == 0)
       continue search;
    // found a prime!
    document.querySelector('#status').innerHTML = n;
  }
}


function buildSpinner(data) {
  
  var canvas = document.createElement('canvas');
  canvas.height = 200;
  canvas.width = 300;
  document.querySelector('article').appendChild(canvas);
  var ctx = canvas.getContext("2d"),
    i = 0, degrees = data.degrees, loops = 0, degreesList = [];
    
  for (i = 0; i < degrees; i++) {
    degreesList.push(i);
  }
  
  // reset
  i = 0;
  
  // so I can kill it later
  window.canvasTimer = setInterval(draw, 1000/degrees);  

  function reset() {
    ctx.clearRect(0,0,100,100); // clear canvas
    
    var left = degreesList.slice(0, 1);
    var right = degreesList.slice(1, degreesList.length);
    degreesList = right.concat(left);
  }
  
  function draw() {
    var c, s, e;

    var d = 0;

    if (i == 0) {
      reset();
    }

    ctx.save();

    d = degreesList[i];
    c = Math.floor(255/degrees*i);
    ctx.strokeStyle = 'rgb(' + c + ', ' + c + ', ' + c + ')';
    ctx.lineWidth = data.size;
    ctx.beginPath();
    s = Math.floor(360/degrees*(d));
    e = Math.floor(360/degrees*(d+1)) - 1;

    ctx.arc(data.x, data.y, data.size, (Math.PI/180)*s, (Math.PI/180)*e, false);
    ctx.stroke();

    ctx.restore();

    i++;
    if (i >= degrees) {
      i = 0;
    }
  }  
}


</script>
